package com.htht.job.executor.util.downLoad;

import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileFilter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by zzj on 2018/1/15.
 */
public class FileUtil {
	 public List<String> getDataFileList(String path, final String pattern) throws IOException {
	        List<String> filelist = new ArrayList<String>();
	        File files=new File(path);
	        File[] list = files.listFiles( new FileFilter() {
				
				@Override
				public boolean accept(File f) {
					if(pattern == null || pattern.equals("")){
						return true;
					}
					
					Pattern p = Pattern.compile(pattern);
					if (p.matcher(f.getName()).find()) {
						return true;
					}
					return false;
				}
			});
	        if (list!=null) {
	            for (File file : list) {
	                filelist.add(file.getName());
	            }
	        }
	        return filelist;
	    }
	 /**
	  * 
	  * @param formPath 原路径
	  * @param filterRules 文件正则
	  * @param timeFormat 时间正则
	  * @param existFileList 数据库中存在的文件
	  * @return
	  * @throws IOException
	  */
    public File[] getDataFileList1(String formPath, final String filterRules,List<String>existFileList) throws IOException {
        List<String> filelist = new ArrayList<String>();
        File files=new File(formPath);
        File[] list = files.listFiles( new FileFilter() {
			@Override
			public boolean accept(File f) {
				if(filterRules == null || filterRules.equals("")){
					return true;
				}
				
				Pattern p = Pattern.compile(filterRules);
				if (p.matcher(f.getName()).find()) {
						return true;
				}
				return false;
			}
		});

		for (File f : list) {
			if (f.isDirectory()) {
				continue;
			} else {
				//验证下后的路径和原文件名
				if (existFileList != null && existFileList.contains(formPath+"#"+f.getName())) {
					continue;
				}
			}
		}
        return list;
    }
    public  void downloadFile(String basePath, String localPath,String fileName,String newFileName){
        try {
            File oldfile = new File(basePath+ "/" + fileName);
            File newfile = new File(localPath+"/"+newFileName);
            int bytesum = 0;
            int byteread = 0;
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(basePath + "/" + fileName); //读入原文件
                if(!newfile.getParentFile().exists()) {
                    newfile.getParentFile().mkdirs();
                }
                FileOutputStream fs = new FileOutputStream(localPath+"/"+newFileName);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }
}
